package com.lauor.test;

import com.lauor.smpedr.session.SqlSessionFactory;
import com.lauor.test.entity.User;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.nio.charset.Charset;
import java.sql.Time;
import java.util.Calendar;
import java.util.Random;

/**
 *springboot测试父类
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@WebAppConfiguration
public class BaseSpringBootUser {
    @Autowired
    protected SqlSessionFactory sqlSessionFactory;

    @Before
    public void beforeTest(){
        System.out.println("before test");
    }

    @After
    public void afterTest(){
        System.out.println("after test");
    }

    protected User buildUserMsg(){
        User user = new User();
        user.setAsset(1_2345_6789L);
        user.setJobNumber( new Random().nextInt(838_8607) );
        user.setHeight((short) 170);
        user.setIsDel((byte) 0);

        user.setVision(1.2f);
        user.setWeight(54.65);

        Calendar birthCalender = Calendar.getInstance();
        birthCalender.set(Calendar.YEAR, 1997);

        user.setBirthDate( birthCalender.getTime() );
        user.setBirthTime( new Time( System.currentTimeMillis() ) );
        user.setCreateTime( Calendar.getInstance().getTime() );
        user.setLastTime( user.getCreateTime() );

        user.setPhone( this.getRandomPhone() );
        user.setName( "name" + new Random().nextInt(10) );
        user.setIntro( "intro" + new Random().nextInt(10) );
        user.setIconImg( "big Avatar".getBytes( Charset.forName("utf-8") ) );

        return user;
    }

    protected String getRandomPhone(){
        int suffixNum = new Random().nextInt(10);
        return "1897579421" + suffixNum;
    }
}
